﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LiveNet.Contacts
{
    public class Owner : DictionaryBase
    {
        #region Private variables
        // Collections
        private ProfileCollection m_profiles = new ProfileCollection();
        private EmailCollection m_emails = new EmailCollection();
        private PhoneCollection m_phones = new PhoneCollection();
        private LocationCollection m_locations = new LocationCollection();
        private URICollection m_uris = new URICollection();
        private bool m_changed = false;
        #endregion

        #region Constructors
        internal Owner()
        {
            BuildDictionary();
        }

        internal Owner(XmlNode xmlnode)
        {
            BuildDictionary();
            FromXml(xmlnode);
        }

        private void BuildDictionary()
        {
            this.Add("WindowsLiveID", "");
            this.Add("CID", "");
        }
        #endregion

        #region Public Properties
        public string WindowsLiveID
        {
            get { return this["WindowsLiveID"]; }
            set { this["WindowsLiveID"] = value; }
        }

        public string CID
        {
            get { return this["CID"]; }
        }

        public ProfileCollection Profiles
        {
            get { return m_profiles; }
            set { m_profiles = value; }
        }

        public EmailCollection Emails
        {
            get { return m_emails; }
            set { m_emails = value; }
        }

        public PhoneCollection Phones
        {
            get { return m_phones; }
            set { m_phones = value; }
        }

        public LocationCollection Locations
        {
            get { return m_locations; }
            set { m_locations = value; }
        }

        public URICollection URIs
        {
            get { return m_uris; }
            set { m_uris = value; }
        }
        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(string.Format("<Owner><WindowsLiveID>{0}</WindowsLiveID>{1}{2}{3}{4}{5}</Owner>", WindowsLiveID, Profiles.ToXml(op).InnerXml, Emails.ToXml(op).InnerXml, Phones.ToXml(op).InnerXml, Locations.ToXml(op).InnerXml, URIs.ToXml(op).InnerXml));
            return xmldoc;
        }
        #endregion

        #region Internal Methods
        internal void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNode node = xmlnode.SelectSingleNode("WindowsLiveID");
            if (node != null) this["WindowsLiveID"] = node.InnerText;
            node = xmlnode.SelectSingleNode("CID");
            if (node != null) this["CID"] = node.InnerText;
            Profiles.FromXml(xmlnode.SelectSingleNode("Profiles"));
            Emails.FromXml(xmlnode.SelectSingleNode("Emails"));
            Phones.FromXml(xmlnode.SelectSingleNode("Phones"));
            Locations.FromXml(xmlnode.SelectSingleNode("Locations"));
            URIs.FromXml(xmlnode.SelectSingleNode("URIs"));
        }
        #endregion

        #region Dictionary Methods
        public String this[String key]
        {
            get { return ((String)Dictionary[key]); }
            set { Dictionary[key] = value; }
        }

        public ICollection Keys
        {
            get { return (Dictionary.Keys); }
        }

        public ICollection Values
        {
            get { return (Dictionary.Values); }
        }

        public void Add(String key, String value)
        {
            Dictionary.Add(key, value);
        }

        public bool Contains(String key)
        {
            return (Dictionary.Contains(key));
        }

        public void Remove(String key)
        {
            Dictionary.Remove(key);
        }

        protected override void OnInsert(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }

        protected override void OnRemove(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");
        }

        protected override void OnSet(Object key, Object oldValue, Object newValue)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (newValue.GetType() != typeof(System.String))
                throw new ArgumentException("newValue must be of type String.", "newValue");
        }

        protected override void OnValidate(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }
        #endregion
    }
}
